package cc.customer.bigEmport.services.imp;

import cc.customer.bigEmport.damain.IpRecords;
import cc.customer.bigEmport.services.BigEmpIrecords;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service("bigEmpIpRecordsServices")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
public class BigEmpIrecordImp extends BaseService<IpRecords> implements BigEmpIrecords {

    private  static  final Logger logger = LoggerFactory.getLogger(BigEmpIrecordImp.class);


    @Override
    public List<IpRecords> getIpRecords(IpRecords records) {

        try {
            Example example = new Example(IpRecords.class);
            Example.Criteria criteria = example.createCriteria();
            //筛选条件
            /**大资源批次*/
            String  ipEname  = records.getcIpEname();
            if(!StringUtils.isEmpty(ipEname)){
                criteria.andLike("cIpEname","%"+ipEname+"%");
            }
            /**所属专员**/
            String ipDirector = records.getcIpDirector();
            if (!StringUtils.isEmpty(ipDirector)){
                criteria.andLike("cIpDirector","%"+ipDirector+"%");
            }
            /**导出批次**/
            String epName = records.getcIpIname();
            if (!StringUtils.isEmpty(epName)){
                criteria.andLike("cIpIname","%"+epName+"%");
            }
            /**批次导出时间选择**/
            String  starttime = records.getStarttime();
            String  endtime = records.getEndtime();

            if (!StringUtils.isAnyEmpty(starttime,endtime)){
                criteria.andBetween("cIpTime",starttime,endtime+" 23:59:59");
            }else if (!StringUtils.isEmpty(starttime)){
                criteria.andCondition("cIpTime = ",starttime);
            }
            /**是否筛选**/
            String cIpProject = records.getcIpProject();
            if (!StringUtils.isEmpty(cIpProject)){
                criteria.andEqualTo("cIpProject",cIpProject);
            }
            /**是否结算**/
            String cIpIsover = records.getcIpIsover();
            if (!StringUtils.isEmpty(cIpIsover)){
                criteria.andEqualTo("cIpIsover",cIpIsover);
            }
            /**是否忽略*/
            String cIpStatus = records.getcIpStatus();
            if (!StringUtils.isEmpty(cIpStatus)){
                criteria.andEqualTo("cIpStatus",cIpStatus);
            }
            criteria.andEqualTo("cIpType","2");
            example.setOrderByClause("c_ip_time desc");
           List<IpRecords> listAll =  this.selectByExample(example);
           return listAll;
        }catch (Exception e){
            logger.error("获取大名单资源导出记录失败:"+e.getMessage());
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    public List<IpRecords> getAiIpRecords(IpRecords records) {
        try {
            Example example = new Example(IpRecords.class);
            Example.Criteria criteria = example.createCriteria();
            //筛选条件
            /**大资源批次*/
            String  ipEname  = records.getcIpEname();
            if(!StringUtils.isEmpty(ipEname)){
                criteria.andLike("cIpEname","%"+ipEname+"%");
            }
            /**所属专员**/
            String ipDirector = records.getcIpDirector();
            if (!StringUtils.isEmpty(ipDirector)){
                criteria.andLike("cIpDirector","%"+ipDirector+"%");
            }
            /**导出批次**/
            String epName = records.getcIpIname();
            if (!StringUtils.isEmpty(epName)){
                criteria.andLike("cIpIname","%"+epName+"%");
            }
            /**批次导出时间选择**/
            String  starttime = records.getStarttime();
            String  endtime = records.getEndtime();

            if (!StringUtils.isAnyEmpty(starttime,endtime)){
                criteria.andBetween("cIpTime",starttime,endtime+" 23:59:59");
            }else if (!StringUtils.isEmpty(starttime)){
                criteria.andCondition("cIpTime = ",starttime);
            }
            /**是否筛选**/
            String cIpProject = records.getcIpProject();
            if (!StringUtils.isEmpty(cIpProject)){
                criteria.andEqualTo("cIpProject",cIpProject);
            }
            /**是否结算**/
            String cIpIsover = records.getcIpIsover();
            if (!StringUtils.isEmpty(cIpIsover)){
                criteria.andEqualTo("cIpIsover",cIpIsover);
            }
            /**是否忽略*/
            String cIpStatus = records.getcIpStatus();
            if (!StringUtils.isEmpty(cIpStatus)){
                criteria.andEqualTo("cIpStatus",cIpStatus);
            }
            criteria.andEqualTo("cIpType","1");
            example.setOrderByClause("c_ip_time desc");
            List<IpRecords> listAll =  this.selectByExample(example);
            return listAll;
        }catch (Exception e){
            logger.error("获取AI名单资源导出记录失败:"+e.getMessage());
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    @Transactional
    public IpRecords getexportBatchCustomer(Long exportid) {
        try {
            //查询当前批次的记录
            IpRecords ipRecords = this.selectByKey(Integer.valueOf(exportid+""));
             return ipRecords;
        }catch (Exception e){
            logger.error("导出批次获取人员失败:"+e.getMessage());
        }
        return null;
    }

    @Override
    @Transactional
    public ResponseBo updateExportBatchCustomer(IpRecords records) {
        try {
            this.updateNotNull(records);
            return ResponseBo.ok("操作成功");
        }catch (Exception e){
            logger.error("更新导出批次状态失败 ："+e.getMessage());
        }
        return ResponseBo.error("操作失败");
    }
}
